/**
 * 
 */
package com.woupla.company;

import java.util.Date;

import javax.crypto.spec.PSource;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import com.woupla.model.Planing;



/**
 * @author Dimitri Lebel
 * 
 */
public class PlaningSemaine {
	
	private Planing planing;

	private Date lunMatDeb;
	private Date lunMatFin;
	private Date lunApDeb;
	private Date lunApFin;

	private Date MarMatDeb;
	private Date MarMatFin;
	private Date MarApDeb;
	private Date MarApFin;

	private Date MerMatDeb;
	private Date MerMatFin;
	private Date MerApDeb;
	private Date MerApFin;

	private Date JeuMatDeb;
	private Date JeuMatFin;
	private Date JeuApDeb;
	private Date JeuApFin;

	private Date VenMatDeb;
	private Date VenMatFin;
	private Date VenApDeb;
	private Date VenApFin;

	private Date SamMatDeb;
	private Date SamMatFin;
	private Date SamApDeb;
	private Date SamApFin;

	private Date DimMatDeb;
	private Date DimMatFin;
	private Date DimApDeb;
	private Date DimApFin;
	
	private int realIndex;

	/**
	 * @return the planing
	 */
	public Planing getPlaning() {
		StringBuffer lundi = ConvertHourInPlaningDay(lunMatDeb,lunMatFin,lunApDeb,lunApFin);	
		planing.setLundi(lundi.toString());
		StringBuffer mardi = ConvertHourInPlaningDay(MarMatDeb,MarMatFin,MarApDeb,MarApFin);	
		planing.setMardi(mardi.toString());
		StringBuffer mercredi = ConvertHourInPlaningDay(MerMatDeb,MerMatFin,MerApDeb,MerApFin);	
		planing.setMercredi(mercredi.toString());
		StringBuffer jeudi = ConvertHourInPlaningDay(JeuMatDeb,JeuMatFin,JeuApDeb,JeuApFin);	
		planing.setJeudi(jeudi.toString());
		StringBuffer vendredi = ConvertHourInPlaningDay(VenMatDeb,VenMatFin,VenApDeb,VenApFin);	
		planing.setVendredi(vendredi.toString());
		StringBuffer samedi = ConvertHourInPlaningDay(SamMatDeb,SamMatFin,SamApDeb,SamApFin);	
		planing.setSamedi(samedi.toString());
		StringBuffer dimanche = ConvertHourInPlaningDay(DimMatDeb,DimMatFin,DimApDeb,DimApFin);	
		planing.setDimanche(dimanche.toString());
		return planing;
	}

	/**
	 * @return
	 */
	private StringBuffer ConvertHourInPlaningDay(Date matDeb, Date matFin, Date apDeb, Date apFin) {
		DateTime dateTime;
		int debMat = -1;
		int finMat = -1;
		int debAp = -1;
		int finAp = -1;
		if(matDeb!= null){
			dateTime = new DateTime(matDeb);
			debMat = ( dateTime.getMinuteOfDay())/5;
		}
		if(matFin!= null){
			dateTime = new DateTime(matFin);
			finMat = ( dateTime.getMinuteOfDay())/5;
		}
		if(apDeb!= null){
			dateTime = new DateTime(apDeb);
			debAp =( dateTime.getMinuteOfDay())/5;
		}
		if(apFin!= null){
			dateTime = new DateTime(apFin);
			finAp = ( dateTime.getMinuteOfDay())/5;
		}	
		StringBuffer planingDay = new StringBuffer();
		for (int i = 0; i < 255; i++) {
			if(i<debMat ){
				planingDay.append("0");
			}
			else if (i>= finAp && finAp>0){
				planingDay.append("0");
			}
			else if (i>=finMat && i<debAp){
				planingDay.append("0");
			}
			else if(debMat<0){
				planingDay.append("0");
			}			
			else{
				planingDay.append("1");
			}
		}
		return planingDay;
	}

	/**
	 * @param planing the planing to set
	 */
	public void setPlaning(Planing planing) {
		this.planing = planing;
		
		StringBuffer lundi = new StringBuffer();
		lundi.append(planing.getLundi());	
		lunMatDeb = getHeureDebMat(lundi);
		lunMatFin = getHeureFinMat(lundi);
		lunApDeb = getHeureDebAp(lundi);
		lunApFin = getHeureFinAp(lundi);
		
		StringBuffer mardi = new StringBuffer();
		mardi.append(planing.getMardi());	
		MarMatDeb = getHeureDebMat(mardi);
		MarMatFin = getHeureFinMat(mardi);
		MarApDeb = getHeureDebAp(mardi);
		MarApFin = getHeureFinAp(mardi);
		
		StringBuffer mercredi = new StringBuffer();
		mercredi.append(planing.getMercredi());	
		MerMatDeb = getHeureDebMat(mercredi);
		MerMatFin = getHeureFinMat(mercredi);
		MerApDeb = getHeureDebAp(mercredi);
		MerApFin = getHeureFinAp(mercredi);
		
		StringBuffer jeudi = new StringBuffer();
		jeudi.append(planing.getJeudi());	
		JeuMatDeb = getHeureDebMat(jeudi);
		JeuMatFin = getHeureFinMat(jeudi);
		JeuApDeb = getHeureDebAp(jeudi);
		JeuApFin = getHeureFinAp(jeudi);
		
		StringBuffer vendredi = new StringBuffer();
		vendredi.append(planing.getVendredi());	
		VenMatDeb = getHeureDebMat(vendredi);
		VenMatFin = getHeureFinMat(vendredi);
		VenApDeb = getHeureDebAp(vendredi);
		VenApFin = getHeureFinAp(vendredi);
		
		StringBuffer samedi = new StringBuffer();
		samedi.append(planing.getSamedi());	
		SamMatDeb = getHeureDebMat(samedi);
		SamMatFin = getHeureFinMat(samedi);
		SamApDeb = getHeureDebAp(samedi);
		SamApFin = getHeureFinAp(samedi);
		
		StringBuffer dimanche = new StringBuffer();
		dimanche.append(planing.getDimanche());	
		DimMatDeb = getHeureDebMat(dimanche);
		DimMatFin = getHeureFinMat(dimanche);
		DimApDeb = getHeureDebAp(dimanche);
		DimApFin = getHeureFinAp(dimanche);
			
	}
	
	public Date getHeureDebMat(StringBuffer planingDay){
		realIndex=0;
		int positionDeb = planingDay.indexOf("1");
		if(realIndex+positionDeb >= 13*60/5 || realIndex+positionDeb < 1){
			return null;
		}
		DateTime dateTime = ConvertInDateTime(planingDay, positionDeb);
		if(dateTime.getHourOfDay()>13)
			return null;
		else{
			return dateTime.toDate();
		}
			
	}	
	
	public Date getHeureFinMat(StringBuffer planingDay){
		int positionDeb = planingDay.indexOf("0");
		if(realIndex+positionDeb >= 13*60/5  || realIndex+positionDeb < 1){
			return null;
		}
		DateTime dateTime = ConvertInDateTime(planingDay, positionDeb);
		if(dateTime.getHourOfDay()>13)
			return null;
		else
			return dateTime.toDate();
	}
	
	public Date getHeureDebAp(StringBuffer planingDay){
		int positionDeb = planingDay.indexOf("1");	
		if(realIndex+positionDeb < 13*60/5  || realIndex+positionDeb < 1){
			return null;
		}
		DateTime dateTime = ConvertInDateTime(planingDay, positionDeb);
		if(dateTime.getHourOfDay()<13)
			return null;
		else
			return dateTime.toDate();
	}
	public Date getHeureFinAp(StringBuffer planingDay){
		int positionDeb = planingDay.indexOf("0");
		if(realIndex+positionDeb < 13*60/5  || realIndex+positionDeb < 1){
			return null;
		}
		DateTime dateTime = ConvertInDateTime(planingDay, positionDeb);
		if(dateTime.getHourOfDay()<13)
			return null;
		else
			return dateTime.toDate();
	}
	
	private DateTime ConvertInDateTime(StringBuffer planingDay, int positionDeb) {
		realIndex = realIndex+positionDeb;
		planingDay = planingDay.delete(0, positionDeb);
		Double hour = Math.floor((realIndex * 5) / 60);
		DateTime dateTime =  new DateTime();
		dateTime = dateTime.withHourOfDay(hour.intValue());
		int min = ((realIndex * 5) % 60);
		dateTime = dateTime.withMinuteOfHour(min);
		dateTime =dateTime.withSecondOfMinute(0);
		dateTime = dateTime.withMillisOfSecond(0);
		return dateTime;
	}
	
	public PlaningSemaine() {
		/*lunMatDeb = new Date();
		lunMatFin = new Date();
		lunApDeb = new Date();
		lunApFin = new Date();

		MarMatDeb = new Date();
		MarMatFin = new Date();
		MarApDeb = new Date();
		MarApFin = new Date();

		MerMatDeb = new Date();
		MerMatFin = new Date();
		MerApDeb = new Date();
		MerApFin = new Date();

		JeuMatDeb = new Date();
		JeuMatFin = new Date();
		JeuApDeb = new Date();
		JeuApFin = new Date();

		VenMatDeb = new Date();
		VenMatFin = new Date();
		VenApDeb = new Date();
		VenApFin = new Date();

		SamMatDeb = new Date();
		SamMatFin = new Date();
		SamApDeb = new Date();
		SamApFin = new Date();

		DimMatDeb = new Date();
		DimMatFin = new Date();
		DimApDeb = new Date();
		DimApFin = new Date();*/
	}

	
	
	/**
	 * @return the lunMatDeb
	 */
	public Date getLunMatDeb() {
		return lunMatDeb;
	}

	/**
	 * @param lunMatDeb the lunMatDeb to set
	 */
	public void setLunMatDeb(Date lunMatDeb) {
		this.lunMatDeb = lunMatDeb;
	}

	/**
	 * @return the lunMatFin
	 */
	public Date getLunMatFin() {
		return lunMatFin;
	}

	/**
	 * @param lunMatFin the lunMatFin to set
	 */
	public void setLunMatFin(Date lunMatFin) {
		this.lunMatFin = lunMatFin;
	}

	/**
	 * @return the lunApDeb
	 */
	public Date getLunApDeb() {
		return lunApDeb;
	}

	/**
	 * @param lunApDeb the lunApDeb to set
	 */
	public void setLunApDeb(Date lunApDeb) {
		this.lunApDeb = lunApDeb;
	}

	/**
	 * @return the lunApFin
	 */
	public Date getLunApFin() {
		return lunApFin;
	}

	/**
	 * @param lunApFin the lunApFin to set
	 */
	public void setLunApFin(Date lunApFin) {
		this.lunApFin = lunApFin;
	}

	/**
	 * @return the marMatDeb
	 */
	public Date getMarMatDeb() {
		return MarMatDeb;
	}

	/**
	 * @param marMatDeb the marMatDeb to set
	 */
	public void setMarMatDeb(Date marMatDeb) {
		MarMatDeb = marMatDeb;
	}

	/**
	 * @return the marMatFin
	 */
	public Date getMarMatFin() {
		return MarMatFin;
	}

	/**
	 * @param marMatFin the marMatFin to set
	 */
	public void setMarMatFin(Date marMatFin) {
		MarMatFin = marMatFin;
	}

	/**
	 * @return the marApDeb
	 */
	public Date getMarApDeb() {
		return MarApDeb;
	}

	/**
	 * @param marApDeb the marApDeb to set
	 */
	public void setMarApDeb(Date marApDeb) {
		MarApDeb = marApDeb;
	}

	/**
	 * @return the marApFin
	 */
	public Date getMarApFin() {
		return MarApFin;
	}

	/**
	 * @param marApFin the marApFin to set
	 */
	public void setMarApFin(Date marApFin) {
		MarApFin = marApFin;
	}

	/**
	 * @return the merMatDeb
	 */
	public Date getMerMatDeb() {
		return MerMatDeb;
	}

	/**
	 * @param merMatDeb the merMatDeb to set
	 */
	public void setMerMatDeb(Date merMatDeb) {
		MerMatDeb = merMatDeb;
	}

	/**
	 * @return the merMatFin
	 */
	public Date getMerMatFin() {
		return MerMatFin;
	}

	/**
	 * @param merMatFin the merMatFin to set
	 */
	public void setMerMatFin(Date merMatFin) {
		MerMatFin = merMatFin;
	}

	/**
	 * @return the merApDeb
	 */
	public Date getMerApDeb() {
		return MerApDeb;
	}

	/**
	 * @param merApDeb the merApDeb to set
	 */
	public void setMerApDeb(Date merApDeb) {
		MerApDeb = merApDeb;
	}

	/**
	 * @return the merApFin
	 */
	public Date getMerApFin() {
		return MerApFin;
	}

	/**
	 * @param merApFin the merApFin to set
	 */
	public void setMerApFin(Date merApFin) {
		MerApFin = merApFin;
	}

	/**
	 * @return the jeuMatDeb
	 */
	public Date getJeuMatDeb() {
		return JeuMatDeb;
	}

	/**
	 * @param jeuMatDeb the jeuMatDeb to set
	 */
	public void setJeuMatDeb(Date jeuMatDeb) {
		JeuMatDeb = jeuMatDeb;
	}

	/**
	 * @return the jeuMatFin
	 */
	public Date getJeuMatFin() {
		return JeuMatFin;
	}

	/**
	 * @param jeuMatFin the jeuMatFin to set
	 */
	public void setJeuMatFin(Date jeuMatFin) {
		JeuMatFin = jeuMatFin;
	}

	/**
	 * @return the jeuApDeb
	 */
	public Date getJeuApDeb() {
		return JeuApDeb;
	}

	/**
	 * @param jeuApDeb the jeuApDeb to set
	 */
	public void setJeuApDeb(Date jeuApDeb) {
		JeuApDeb = jeuApDeb;
	}

	/**
	 * @return the jeuApFin
	 */
	public Date getJeuApFin() {
		return JeuApFin;
	}

	/**
	 * @param jeuApFin the jeuApFin to set
	 */
	public void setJeuApFin(Date jeuApFin) {
		JeuApFin = jeuApFin;
	}

	/**
	 * @return the venMatDeb
	 */
	public Date getVenMatDeb() {
		return VenMatDeb;
	}

	/**
	 * @param venMatDeb the venMatDeb to set
	 */
	public void setVenMatDeb(Date venMatDeb) {
		VenMatDeb = venMatDeb;
	}

	/**
	 * @return the venMatFin
	 */
	public Date getVenMatFin() {
		return VenMatFin;
	}

	/**
	 * @param venMatFin the venMatFin to set
	 */
	public void setVenMatFin(Date venMatFin) {
		VenMatFin = venMatFin;
	}

	/**
	 * @return the venApDeb
	 */
	public Date getVenApDeb() {
		return VenApDeb;
	}

	/**
	 * @param venApDeb the venApDeb to set
	 */
	public void setVenApDeb(Date venApDeb) {
		VenApDeb = venApDeb;
	}

	/**
	 * @return the venApFin
	 */
	public Date getVenApFin() {
		return VenApFin;
	}

	/**
	 * @param venApFin the venApFin to set
	 */
	public void setVenApFin(Date venApFin) {
		VenApFin = venApFin;
	}

	/**
	 * @return the samMatDeb
	 */
	public Date getSamMatDeb() {
		return SamMatDeb;
	}

	/**
	 * @param samMatDeb the samMatDeb to set
	 */
	public void setSamMatDeb(Date samMatDeb) {
		SamMatDeb = samMatDeb;
	}

	/**
	 * @return the samMatFin
	 */
	public Date getSamMatFin() {
		return SamMatFin;
	}

	/**
	 * @param samMatFin the samMatFin to set
	 */
	public void setSamMatFin(Date samMatFin) {
		SamMatFin = samMatFin;
	}

	/**
	 * @return the samApDeb
	 */
	public Date getSamApDeb() {
		return SamApDeb;
	}

	/**
	 * @param samApDeb the samApDeb to set
	 */
	public void setSamApDeb(Date samApDeb) {
		SamApDeb = samApDeb;
	}

	/**
	 * @return the samApFin
	 */
	public Date getSamApFin() {
		return SamApFin;
	}

	/**
	 * @param samApFin the samApFin to set
	 */
	public void setSamApFin(Date samApFin) {
		SamApFin = samApFin;
	}

	/**
	 * @return the dimMatDeb
	 */
	public Date getDimMatDeb() {
		return DimMatDeb;
	}

	/**
	 * @param dimMatDeb the dimMatDeb to set
	 */
	public void setDimMatDeb(Date dimMatDeb) {
		DimMatDeb = dimMatDeb;
	}

	/**
	 * @return the dimMatFin
	 */
	public Date getDimMatFin() {
		return DimMatFin;
	}

	/**
	 * @param dimMatFin the dimMatFin to set
	 */
	public void setDimMatFin(Date dimMatFin) {
		DimMatFin = dimMatFin;
	}

	/**
	 * @return the dimApDeb
	 */
	public Date getDimApDeb() {
		return DimApDeb;
	}

	/**
	 * @param dimApDeb the dimApDeb to set
	 */
	public void setDimApDeb(Date dimApDeb) {
		DimApDeb = dimApDeb;
	}

	/**
	 * @return the dimApFin
	 */
	public Date getDimApFin() {
		return DimApFin;
	}

	/**
	 * @param dimApFin the dimApFin to set
	 */
	public void setDimApFin(Date dimApFin) {
		DimApFin = dimApFin;
	}

}
